package com.dengyafei.controller;

import com.dengyafei.bean.ItemEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.vo.ItemAddVO;
import com.dengyafei.domin.vo.QueryItemsVO;
import com.dengyafei.result.PageEntity;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 邓亚非
 */
@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    ItemService itemService;


    /**
     * 查询所有的items
     *
     * @return
     */
    @PostMapping(value = "/query")
    public RestApiResult queryAllItems(@RequestBody QueryItemsVO queryItemsVO) {
        System.out.println("condition:" + queryItemsVO.getCondition());
        System.out.println("current:" + queryItemsVO.getPageSize());
        System.out.println("pageSize:" + queryItemsVO.getCurrent());
        PageEntity<ItemEntity> pageEntity = itemService.queryAllItem(queryItemsVO);
        List<ItemEntity> list = pageEntity.getList();
        return RestApiResult.ok(pageEntity);
    }

    /**
     * 添加一个物品
     *
     * @param itemAddVO
     * @return
     */
    @PostMapping(value = "/add")
    public RestApiResult addOneItem(@RequestBody ItemAddVO itemAddVO) {
        log.info("【添加物品接口】itemAddVO:[{}]", itemAddVO);
        Integer integer = itemService.addOneItem(itemAddVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【添加物品接口-添加失败】itemAddVO:[{}]", ResultMessage.ADD_FAILURE);
            return RestApiResult.error(ResultMessage.ADD_FAILURE);
        }
        log.info("【添加物品接口-添加成功】itemAddVO:[{}]", ResultMessage.ADD_SUCCESS);
        return RestApiResult.ok(ResultMessage.RESPONSE_SUCCESS_STATUS, ResultMessage.ADD_SUCCESS);
    }
}